package models

import (
	"fmt"
	"mindonmap/pkgs/util"
	"time"

	"github.com/jinzhu/gorm"
)

type MindFlow struct {
	Model
	Id             int    `json:"-"`
	State          int    `json:"state"`
	Acid           int    `json:"acid"`
	Mindid         string `json:"mindid"`
	Filename       string `json:"filename"`
	Image          string `json:"image"`
	Sharestate     int    `json:"sharestate"`
	UserStatus     int    `json:"user_status"`
	Createtime     int    `json:"createtime"`
	CreatePlatform string `json:"create_platform"`
	SavePlatform   string `json:"save_platform"`
}
type MindflowData struct {
	Model
	Id      int    `json:"-"`
	Pid     string `json:"pid"`
	Acid    int    `json:"acid"`
	Image   string `json:"image"`
	Mapdata string `json:"mapdata"`
}
type MindFlowlist struct {
	Id         int    `json:"-"`
	State      int    `json:"state"`
	Acid       int    `json:"acid"`
	Mindid     string `json:"mindid"`
	Filename   string `json:"filename"`
	Image      string `json:"image"`
	Sharestate int    `json:"sharestate"`
	Checktype  int    `json:"checktype"`
	Createtime int    `json:"createtime"`
	T          string `json:"t"`
}

func (tag *MindFlow) BeforeCreate(scope *gorm.Scope) error {
	scope.SetColumn("createtime", time.Now().Unix())

	return nil
}
func (tag *MindFlow) BeforeUpdate(scope *gorm.Scope) error {
	scope.SetColumn("modifietime", time.Now().Unix())
	return nil
}

func GetMindFlow(mindid string) (minds MindFlow) {
	db.Where("mindid = ?", mindid).First(&minds)
	//db.Model(&minds).Related(&minds.Tag)
	return
}

func CopyFlow(mindid string) (minds MindFlow) {
	var Mind_flow_data MindflowData
	db.Where("mindid = ?", mindid).First(&minds)
	db.Where("pid = ?", mindid).First(&Mind_flow_data)
	uuidStr, _ := util.UUIDv4()
	db.Create(&MindFlow{
		Acid:     minds.Acid,
		Mindid:   uuidStr,
		Filename: minds.Filename + "_copy",
		Image:    minds.Image,
		State:    minds.State,
	})
	db.Create(&MindflowData{
		Acid:    minds.Acid,
		Pid:     uuidStr,
		Mapdata: Mind_flow_data.Mapdata,
	})
	return
}

func GetMindFlowData(mindid string) (minds MindflowData) {
	db.Where("pid = ?", mindid).First(&minds)
	//db.Model(&minds).Related(&minds.Tag)
	return
}

func GetMindFlowsTempate(PageNum int, PageSize int, maps interface{}) (minds []MindFlow) {
	db.Where(maps).Order("id desc").Offset(PageNum).Limit(PageSize).Find(&minds)
	return
}
func GetMindFlowsDelList(PageNum int, PageSize int, userid int) (minds []MindFlowlist) {
	//db.Where(maps).Order("id desc").Offset(PageNum).Limit(PageSize).Find(&minds)
	db.Raw("(SELECT id,mindid,createtime,filename,image,acid,statetime,\"f\" as t FROM md_mind_flow WHERE state =1 and acid = ?) UNION(SELECT id,mindid,createtime,filename,image,acid,statetime,\"m\" as t FROM md_mindmap WHERE version =1 and state =1 and acid = ?) ", userid, userid).Order("statetime desc").Scan(&minds)
	return
}

func GetMindFlowsDelListV5(PageNum int, PageSize int, userid int) (minds []MindFlowlist) {
	//db.Where(maps).Order("id desc").Offset(PageNum).Limit(PageSize).Find(&minds)
	db.Raw("(SELECT id,mindid,createtime,filename,image,acid,statetime,\"f\" as t FROM md_mind_flow WHERE state =1 and acid = ?) UNION(SELECT id,mindid,createtime,filename,image,acid,statetime,\"m\" as t FROM md_mindmap WHERE state =1 and acid = ?) ", userid, userid).Order("statetime desc").Scan(&minds)
	return
}
func GetMindFlowslist(PageNum int, PageSize int, userid int, order string) (minds []MindFlow) {
	db.Raw("SELECT map.id,map.mindid,shares.shareid,map.createtime,shares.checktype,map.filename,map.image,map.acid,map.sharestate FROM md_mind_flow AS map left JOIN md_mind_share AS shares ON map.mindid = shares.pid WHERE map.state =0 and map.acid = ? ", userid).Order(order).Scan(&minds)
	return
}

func AddMindFlow(data map[string]interface{}) bool {
	var res = db.Create(&MindFlow{
		Acid:           data["acid"].(int),
		Mindid:         data["mindid"].(string),
		Filename:       data["filename"].(string),
		Image:          data["image"].(string),
		State:          data["state"].(int),
		UserStatus:     data["state"].(int),
		CreatePlatform: data["create_platform"].(string),
	})
	if res.Error != nil {
		fmt.Println("Error：", res.Error)
		return false
	}
	return true
}

func EditMindFlow(mindid string, maps interface{}) bool {
	var res = db.Model(&MindFlow{}).Where("mindid = ?", mindid).Update(maps)
	if res.Error != nil {
		fmt.Println("Error：", res.Error)
		return false
	}
	return true
}

func AddMindFlowData(data map[string]interface{}) bool {
	var res = db.Create(&MindflowData{
		Acid:    data["acid"].(int),
		Pid:     data["pid"].(string),
		Mapdata: data["mapdata"].(string),
	})
	if res.Error != nil {
		fmt.Println("Error：", res.Error)
		return false
	}
	return true
}
func EditMindFlowData(mindid string, maps interface{}) bool {
	var res = db.Model(&MindflowData{}).Where("pid = ?", mindid).Update(maps)
	if res.Error != nil {
		fmt.Println("Error：", res.Error)
		return false
	}
	return true
}

func DeleteMindFlow(mindid string, maps interface{}) bool {
	var res = db.Model(&MindFlow{}).Where("mindid = ?", mindid).Update(maps)
	if res.Error != nil {
		fmt.Println("Error：", res.Error)
		return false
	}
	return true
}
func DeleteMindFlows(mindid interface{}, userid int, maps interface{}) bool {
	var res = db.Model(&MindFlow{}).Where("acid = ? and mindid in (?)", userid, mindid).Update(maps)
	if res.Error != nil {
		fmt.Println("Error：", res.Error)
		return false
	}
	return true
}
func DeleteAllMindFlows(userid int, maps interface{}) bool {
	var res = db.Model(&MindFlow{}).Where("acid = ? and state = 1", userid).Update(maps)
	if res.Error != nil {
		fmt.Println("Error：", res.Error)
		return false
	}
	return true
}

func ExistMindFlowByID(mindid string, userid int) bool {
	var minds MindFlow
	db.Where("mindid = ? and acid = ?", mindid, userid).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}
func ExistMindFlowByMindID(id string) bool {
	var minds MindFlow
	db.Where("mindid = ?", id).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}
func ExistMindFlowByPID(id string) bool {
	var minds MindflowData
	db.Where("pid = ?", id).First(&minds)
	if minds.ID > 0 {
		return true
	}
	return false
}

func GetMindFlowTotal(userid int) (minds MindmapTotal) {
	db.Raw("SELECT SUM(mm) AS total FROM ((SELECT count(*) AS mm FROM md_mind_flow	WHERE acid = ? AND state in(0,1)) UNION all (SELECT count(*) AS mm FROM md_mindmap WHERE acid = ? AND state in(0,1))) mm", userid, userid).Find(&minds)
	return
}
